/**
* @projectName im-21675-cqmx1
* @package com.zhang.codec
* @className com.zhang.codec.ObjEncoder
* @copyright Copyright 2020 Thunisoft, Inc All rights reserved.
*/package com.zhang.codec;

import com.zhang.util.SerializationUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
* ObjEncoder
* @description 解码器
* @author zhangjun
* @date 2025/9/19 下午4:48
* @version 1.0
*/
public class ObjEncoder extends MessageToByteEncoder {



    private Class<?> genericClass;



    public ObjEncoder(Class<?> genericClass) {

        this.genericClass = genericClass;

    }



    @Override

    protected void encode(ChannelHandlerContext ctx, Object in, ByteBuf out)  {

        if (genericClass.isInstance(in)) {

            byte[] data = SerializationUtil.serialize(in);

            out.writeInt(data.length);

            out.writeBytes(data);

        }

    }



}